package com.eric.code.oauth2.login;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

/**
 * Created by eric on 2017/3/24.
 */
public class LoginController extends Controller {

    private LoginService srv = LoginService.me;

    public void index(){
        render("login.html");
    }

    @Before(LoginValidator.class)
    @ActionKey("/doLogin")
    public void doLogin(){
        Ret ret = srv.login(getPara("email"), getPara("password"));
        if(ret.isOk()){
            String sessionId = ret.getStr(LoginService.sessionIdName);
            setCookie(LoginService.sessionIdName,sessionId,-1);
            switch (ret.getInt("type")){
                case LoginService.USER_NORMAL:
                    redirect("/my");
                    break;
                case LoginService.USER_ADMIN:
                    redirect("/app");
                    break;
            }
            return ;
        }else{
            setAttr("ret",ret);
            render("/view/error.html");
        }

    }

}
